from PyQt6.QtWidgets import (QGraphicsScene, QGraphicsView, QGraphicsEllipseItem,
                            QGraphicsTextItem)
from PyQt6.QtCore import Qt, QPointF
from PyQt6.QtGui import QPen, QColor, QBrush, QPainter
import math

class BluetoothRadar(QGraphicsView):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.scene = QGraphicsScene(self)
        self.setScene(self.scene)
        self.setRenderHint(QPainter.Antialiasing)
        
        # 雷达参数
        self.center_x = 0
        self.center_y = 0
        self.max_radius = 200  # 最大半径
        self.rings = 4  # 同心圆数量
        
        # 设备点列表
        self.device_items = {}  # 存储设备图形项
        
        self.init_radar()
    
    def init_radar(self):
        """初始化雷达背景"""
        # 清除场景
        self.scene.clear()
        
        # 绘制同心圆
        for i in range(self.rings):
            radius = (i + 1) * (self.max_radius / self.rings)
            circle = QGraphicsEllipseItem(
                -radius, -radius, radius * 2, radius * 2)
            circle.setPen(QPen(QColor(0, 255, 0, 100)))
            self.scene.addItem(circle)
            
            # 添加距离标签
            distance_text = QGraphicsTextItem(f"{int(-30 * (i + 1))}dBm")
            distance_text.setDefaultTextColor(QColor(0, 255, 0))
            distance_text.setPos(5, -radius)
            self.scene.addItem(distance_text)
        
        # 绘制十字线
        self.scene.addLine(-self.max_radius, 0, self.max_radius, 0,
                          QPen(QColor(0, 255, 0, 100)))
        self.scene.addLine(0, -self.max_radius, 0, self.max_radius,
                          QPen(QColor(0, 255, 0, 100)))
        
        # 设置视图属性
        self.setSceneRect(-self.max_radius, -self.max_radius,
                         self.max_radius * 2, self.max_radius * 2)
        self.setHorizontalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAlwaysOff)
        self.setVerticalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAlwaysOff)
    
    def update_device_position(self, address, name, rssi):
        """更新设备位置"""
        # 将RSSI转换为距离（半径）
        # RSSI范围通常在-100到-30之间
        min_rssi = -100
        max_rssi = -30
        radius = ((rssi - max_rssi) / (min_rssi - max_rssi)) * self.max_radius
        
        # 为每个设备分配一个固定的角度（���于地址）
        angle = int(address.replace(':', ''), 16) % 360
        radian = math.radians(angle)
        
        # 计算设备坐标
        x = radius * math.cos(radian)
        y = radius * math.sin(radian)
        
        # 更新或创建设备图形项
        if address in self.device_items:
            device_item, text_item = self.device_items[address]
            device_item.setPos(x, y)
            text_item.setPos(x + 10, y)
        else:
            # 创建设备点
            device_item = QGraphicsEllipseItem(-5, -5, 10, 10)
            device_item.setBrush(QBrush(QColor(255, 0, 0)))
            device_item.setPen(QPen(QColor(255, 0, 0)))
            device_item.setPos(x, y)
            self.scene.addItem(device_item)
            
            # 创建设备标签
            text_item = QGraphicsTextItem(f"{name}\n{rssi}dBm")
            text_item.setDefaultTextColor(QColor(0, 255, 0))
            text_item.setPos(x + 10, y)
            self.scene.addItem(text_item)
            
            self.device_items[address] = (device_item, text_item)
    
    def remove_device(self, address):
        """移除设备"""
        if address in self.device_items:
            device_item, text_item = self.device_items[address]
            self.scene.removeItem(device_item)
            self.scene.removeItem(text_item)
            del self.device_items[address]
    
    def resizeEvent(self, event):
        """处理窗口大小改变"""
        super().resizeEvent(event)
        self.fitInView(self.sceneRect(), Qt.AspectRatioMode.KeepAspectRatio) 